<?php
namespace ZyucMini\Core;

/**
 * 分页类 - 负责分页计算和信息管理
 * 用于处理数据分页，计算分页信息等
 */
class Pager
{
    /**
     * 总记录数
     * @var int
     */
    private $totalItems;
    
    /**
     * 每页记录数
     * @var int
     */
    private $itemsPerPage;
    
    /**
     * 当前页码
     * @var int
     */
    private $currentPage;
    
    /**
     * 总页数
     * @var int
     */
    private $totalPages;
    
    /**
     * 构造函数
     * @param int $totalItems 总记录数
     * @param int $currentPage 当前页码 (从1开始)
     * @param int $itemsPerPage 每页记录数 (默认10)
     */
    public function __construct($totalItems, $currentPage = 1, $itemsPerPage = 10)
    {
        $this->totalItems = max(0, (int)$totalItems);
        $this->itemsPerPage = max(1, (int)$itemsPerPage);
        $this->currentPage = max(1, (int)$currentPage);
        
        $this->totalPages = $this->itemsPerPage > 0 
            ? ceil($this->totalItems / $this->itemsPerPage)
            : 0;
            
        // 确保当前页不超过总页数
        if ($this->currentPage > $this->totalPages && $this->totalPages > 0) {
            $this->currentPage = $this->totalPages;
        }
    }
    
    /**
     * 获取总页数
     * @return int
     */
    public function getTotalPages()
    {
        return $this->totalPages;
    }
    
    /**
     * 获取总记录数
     * @return int
     */
    public function getTotalItems()
    {
        return $this->totalItems;
    }
    
    /**
     * 是否有上一页
     * @return bool
     */
    public function hasPreviousPage()
    {
        return $this->currentPage > 1;
    }
    
    /**
     * 是否有下一页
     * @return bool
     */
    public function hasNextPage()
    {
        return $this->currentPage < $this->totalPages;
    }
    
    /**
     * 获取当前页码
     * @return int
     */
    public function getCurrentPage()
    {
        return $this->currentPage;
    }
    
    /**
     * 获取每页记录数
     * @return int
     */
    public function getItemsPerPage()
    {
        return $this->itemsPerPage;
    }
    
    /**
     * 获取上一页页码
     * @return int|null
     */
    public function getPreviousPage()
    {
        return $this->hasPreviousPage() ? $this->currentPage - 1 : null;
    }
    
    /**
     * 获取下一页页码
     * @return int|null
     */
    public function getNextPage()
    {
        return $this->hasNextPage() ? $this->currentPage + 1 : null;
    }
    
    /**
     * 获取当前页的起始记录索引 (从0开始)
     * @return int
     */
    public function getOffset()
    {
        return ($this->currentPage - 1) * $this->itemsPerPage;
    }
    
    /**
     * 获取当前页的结束记录索引 (从0开始)
     * @return int
     */
    public function getEndOffset()
    {
        return min($this->getOffset() + $this->itemsPerPage - 1, $this->totalItems - 1);
    }
    
    /**
     * 获取当前页显示记录范围描述
     * @return string
     */
    public function getRangeDescription()
    {
        if ($this->totalItems === 0) {
            return '0 条记录';
        }
        
        $start = $this->getOffset() + 1;
        $end = min($this->getOffset() + $this->itemsPerPage, $this->totalItems);
        
        return "第 {$start}-{$end} 条，共 {$this->totalItems} 条记录";
    }
    
    /**
     * 获取分页导航数组
     * @param int $maxPages 最大显示页数 (默认5)
     * @return array
     */
    public function getPageNumbers($maxPages = 5)
    {
        if ($this->totalPages <= 1) {
            return [];
        }
        
        $maxPages = max(3, min($maxPages, $this->totalPages));
        $half = floor($maxPages / 2);
        
        $start = max(1, $this->currentPage - $half);
        $end = min($this->totalPages, $start + $maxPages - 1);
        
        // 调整起始位置
        if ($end - $start + 1 < $maxPages) {
            $start = max(1, $end - $maxPages + 1);
        }
        
        return range($start, $end);
    }
    
    /**
     * 获取所有分页信息
     * @return array
     */
    public function getPagerInfo()
    {
        return [
            'total' => $this->totalItems,
            'pageSize' => $this->itemsPerPage,
            'pageNo' => $this->currentPage,
            'totalPage' => $this->totalPages,
            'prev' => $this->hasPreviousPage(),
            'next' => $this->hasNextPage(),
            'prevPage' => $this->getPreviousPage(),
            'nextPage' => $this->getNextPage(),
            'offset' => $this->getOffset(),
            'endOffset' => $this->getEndOffset(),
            'range' => $this->getRangeDescription(),
            'pageNumbers' => $this->getPageNumbers(),
        ];
    }
    
    /**
     * 检查页码是否有效
     * @param int $page 页码
     * @return bool
     */
    public function isValidPage($page)
    {
        return $page >= 1 && $page <= $this->totalPages;
    }
    
    /**
     * 获取分页统计信息
     * @return array
     */
    public function getStats()
    {
        return [
            'totalItems' => $this->totalItems,
            'itemsPerPage' => $this->itemsPerPage,
            'currentPage' => $this->currentPage,
            'totalPages' => $this->totalPages,
            'hasData' => $this->totalItems > 0,
            'isFirstPage' => $this->currentPage === 1,
            'isLastPage' => $this->currentPage === $this->totalPages,
        ];
    }
}